home *** CD-ROM | disk | FTP | other *** search
/ Light ROM 1 / LIGHT-ROM 1 (Amiga Library Services)(1994).iso / ffdisks / d939.lha / ExtraCmds / source_etc.lha / src / SplitName.c < prev    next >
C/C++ Source or Header  |  1993-10-22  |  3KB  |  131 lines

  1. /*   ---------------------------------      -------
  2.  *   |\  | | | | |  |.| |   \|  |/ /|\      |||||||
  3.  *   | | | |/  | |\ |/  |/|  |\ |/  |    ?  ---+---  =<
  4.  *   | | | |   | |  |     |  |  |   |     \qqqqqqqqq/
  5.  *   ---------------------------------  ~~~~~~~~~~~~~~~~
  6.  *  SPLITNAME - Splits a filename into its parts.
  7.  *  Copyright (C) 1993 Torsten Poulin
  8.  *
  9.  *  This program is free software; you can redistribute it and/or modify
  10.  *  it under the terms of the GNU General Public License as published by
  11.  *  the Free Software Foundation; either version 2 of the License, or
  12.  *  (at your option) any later version.
  13.  *
  14.  *  This program is distributed in the hope that it will be useful,
  15.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  16.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17.  *  GNU General Public License for more details.
  18.  *
  19.  *  You should have received a copy of the GNU General Public License
  20.  *  along with this program; if not, write to the Free Software
  21.  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  22.  *
  23.  *  The author can be contacted by s-mail at
  24.  *    Torsten Poulin
  25.  *    Banebrinken 99, 2, 77
  26.  *    DK-2400 Copenhagen NV
  27.  *    DENMARK
  28.  *
  29.  * $Id: SplitName.c,v 37.2 93/02/08 14:06:29 Torsten Rel $
  30.  * $Log:    SplitName.c,v $
  31.  * Revision 37.2  93/02/08  14:06:29  Torsten
  32.  * Replaced strlen() and rindex() with rlocate() to
  33.  * save an additional pass through the name string
  34.  * as well as a few bytes ;-)
  35.  * 
  36.  * Fixed a bug in the setting of the EXT variable
  37.  * when the name does not contain an extension.
  38.  * 
  39.  * Revision 37.1  93/02/06  18:53:10  Torsten
  40.  * This is the initial revision.
  41.  * 
  42.  */
  43.  
  44. #include <exec/types.h>
  45. #include <dos/dos.h>
  46. #include <dos/var.h>
  47. #include <dos/dostags.h>
  48. #include <proto/exec.h>
  49. #include <proto/dos.h>
  50. #include "splitname_rev.h"
  51.  
  52. #define TEMPLATE "NAME/A,PATH,BASE,EXT=EXTENSION"
  53. #define OPT_NAME 0
  54. #define OPT_PATH 1
  55. #define OPT_BASE 2
  56. #define OPT_EXT  3
  57.  
  58. char const versionID[] = VERSTAG;
  59. char const copyright[] = "$COPYRIGHT: ©1993 Torsten Poulin";
  60. char *rlocate(char *, char);
  61.  
  62. LONG entrypoint(VOID)
  63. {
  64.   struct RDArgs *args;
  65.  
  66.   struct DosLibrary *DOSBase;
  67.   struct Library *SysBase;
  68.  
  69.   LONG arg[4];
  70.   LONG rc = RETURN_OK;
  71.   UBYTE *base, *ext;
  72.  
  73.   SysBase = *(struct Library **) 4L;
  74.   if (!(DOSBase = (struct DosLibrary *) OpenLibrary("dos.library", 37L)))
  75.   {
  76.     rc = RETURN_FAIL;
  77.     goto exit1;
  78.   }
  79.  
  80.   arg[OPT_NAME] = arg[OPT_PATH] = arg[OPT_BASE] = arg[OPT_EXT] = 0L;
  81.  
  82.   if (args = ReadArgs(TEMPLATE, arg, NULL))
  83.   {
  84.     base = FilePart((UBYTE *) arg[OPT_NAME]);
  85.     ext = rlocate(base, '.');
  86.  
  87.     if (arg[OPT_EXT])
  88.       SetVar((UBYTE *) arg[OPT_EXT], ext ? ext : "", -1, GVF_LOCAL_ONLY);
  89.  
  90.     if (arg[OPT_BASE])
  91.     {
  92.       /* Truncate *base at start of extension, if applicable */
  93.       if(ext)
  94.     *ext = '\0';
  95.       SetVar((UBYTE *) arg[OPT_BASE], base, -1, GVF_LOCAL_ONLY);
  96.     }
  97.  
  98.     if (arg[OPT_PATH])
  99.     {
  100.       /* Truncate arg[OPT_NAME] at start of basename */
  101.       *base = '\0';
  102.       SetVar((UBYTE *) arg[OPT_PATH], (UBYTE *) arg[OPT_NAME], -1,
  103.          GVF_LOCAL_ONLY);
  104.     }
  105.  
  106.     FreeArgs(args);
  107.   }
  108.   else
  109.   {
  110.     LONG err = IoErr();
  111.  
  112.     PrintFault(err, "SplitName");
  113.     rc = RETURN_ERROR;
  114.   }
  115.  
  116.   CloseLibrary((struct Library *) DOSBase);
  117. exit1:
  118.   return rc;
  119. }
  120.  
  121.  
  122. char *rlocate(char *s, char c)
  123. {
  124.   char *pos = NULL;
  125.  
  126.   for (; *s; s++)
  127.     if (*s == c)
  128.       pos = s;
  129.   return pos;
  130. }
  131.